package cn.fancy.sdk.service;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import cn.fancy.sdk.common.utils.ResUtil;

class USLodeDialog {
	
	// 显示Loading动画
	static Dialog showWaitingDialog(Context context) 
	{
		return showWaitingDialog(context, null);
	}
	
	static Dialog showWaitingDialog(Context context,
			OnCancelListener listener) 
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		View dialogView = inflater.inflate(ResUtil.getLayoutId(context,"dialog_fancy_loading"), null);
		LinearLayout dialogLayout = (LinearLayout) dialogView
				.findViewById(ResUtil.getId(context,"qdservice_loading_dialog_layout"));
		ImageView iv = (ImageView) dialogView
				.findViewById(ResUtil.getId(context,"qdservice_loading_img"));
		iv.startAnimation(AnimationUtils.loadAnimation(context,
				ResUtil.getAnimId(context,"qdservice_loading_anim")));
		Dialog waitingDialog = new Dialog(context, ResUtil.getStyleId(context,"dialog_fancy_loading"));
		waitingDialog.setContentView(dialogLayout,
				new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT,
						LinearLayout.LayoutParams.MATCH_PARENT));

		waitingDialog.setCancelable(true);
		waitingDialog.setOnCancelListener(listener);
		waitingDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
		waitingDialog.setCanceledOnTouchOutside(true);
		waitingDialog.show();
		return waitingDialog;
	}
	
	// 关闭Loading动画
	static void cancelDialog(Dialog dialog) 
	{
		if(dialog!=null && dialog.isShowing()) {
			dialog.dismiss();
		}
		dialog = null;
	}
	
}
